package com.imooc.rabbitmq.quickstart;

import com.imooc.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();

        //3. 通过Connection 创建一个channel
        Channel channel = connection.createChannel();

        channel.queueDeclare("test001",false,false,false,null);

        /**
         *  new DefaultConsumer() 新的API
         */
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body,"utf-8");
                System.out.println("recv:"+msg);
            }
        };
        //监听队列
        channel.basicConsume("test001",true, consumer);
    }
}
